// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../../../general/Script/component/ComponentBase";
import { g_bundleName } from "../../../../general/Script/mgr/assetsManager";
import DataMgr from "../../../../general/Script/mgr/dataManager";
import GameMgr from "../../../../general/Script/mgr/gameManager";
import NativeMgr from "../../../../general/Script/mgr/nativeManager";
import NetMgr, { g_rankType } from "../../../../general/Script/mgr/netManager";
import SoundMgr from "../../../../general/Script/mgr/soundManager";
import { subGameMgr } from "../../../../general/Script/mgr/SubGameMgr";
import { GameType } from "../content";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BlockOver extends ComponentBase {

    @property([cc.SpriteFrame])
    leftTimeSp: cc.SpriteFrame[] = [];




    timer: cc.Node = null;
    normal: cc.Node = null;
    level: cc.Node = null;


    timeLeftCycle: cc.Node = null;
    timeLeft: cc.Node = null;




    oldScore: cc.Node = null;
    newScore: cc.Node = null;



    reGame: cc.Node = null;
    newGame: cc.Node = null;
    leftVideoTimes: cc.Node = null;
    leftTime = 5;



    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        super.onLoad()

        // this.updateTime()

        this.initEvents();
        const win = DataMgr.Ins().getGameData('win');
        if (win) {
            this.initLevel(win);
        } else {
            this.initTimer();
        }


    }


    initEvents() {

        cc.systemEvent.on('RewardClose', (data) => {
            console.log('--------RewardClose');
        }, this)

        cc.systemEvent.on('RewardArrived', (data) => {
            console.log('------------RewardArrived', data);
            if (data == 'ok') {
                const times = DataMgr.Ins().getLeftVideoTimes();
                DataMgr.Ins().setLeftVideoTimes(times - 1);
                this.unscheduleAllCallbacks();
                this.node.removeFromParent();
                this.node.destroy();
                GameMgr.Ins().emit('reBlockGame');
            }

        }, this)
    }


    initTimer() {

        this.timer.active = true;
        this.normal.active = false;
        this.level.active = false;



        this.initScore();
        return;
        this.leftTime = 5;

        this.updateTime();
        this.schedule(this.updateTime.bind(this), 1, 5, 0);
        this.schedule(this.updateCycle.bind(this), 0.05, 1000, 0);


        const times = DataMgr.Ins().getLeftVideoTimes();

        console.log('times = ', times);
        this.leftVideoTimes.getComponent(cc.Label).string = `${times}/7`
        if (times == 0) {
            this.leftVideoTimes.parent.active = false;
        }
    }


    initScore() {

        this.timer.active = false;
        this.normal.active = true;

        const score = DataMgr.Ins().getGameData('score');
        const highScore = DataMgr.Ins().getGameData('highScore');
        console.log('gameover score', score, highScore);

        NetMgr.Ins().postRankList(g_rankType.block, score);

        if (score > highScore) {
            this.oldScore.active = false;
            this.newScore.active = true;
            DataMgr.Ins().setLocalStorage('highScore', score);
            const scoreNode = this.newScore.getChildByName('score');
            this.showScore(scoreNode);
        } else {
            this.oldScore.active = true;
            this.newScore.active = false;
            const highScoreNode = this.oldScore.getChildByName('high');
            highScoreNode.getComponent(cc.Label).string = highScore.toString();
            const scoreNode = this.oldScore.getChildByName('score');
            this.showScore(scoreNode);
        }


    }

    initLevel(win: boolean) {
        const target = DataMgr.Ins().getGameData('gameTarget');
        const targetLeft = DataMgr.Ins().getGameData('leftTarget');
        this.timer.active = false;
        this.level.active = true;

        const winNode = this.level.getChildByName('win');
        const loseNode = this.level.getChildByName('lose');
        winNode.active = win;
        loseNode.active = !win;
        if (win) {
            // DataMgr.Ins().nowLevel += 1;
            // DataMgr.Ins().setLocalStorage('nowLevel', DataMgr.Ins().nowLevel);
        }

    }

    start() {
        // SoundMgr.Ins().playEffect('game_overScore');
    }

    showScore(scoreNode: cc.Node) {
        SoundMgr.Ins().playEffect('game_over', g_bundleName.gameBlock);
        const score = DataMgr.Ins().getGameData('score');
        scoreNode.getComponent(cc.Label).string = score.toString();
        let s = 0;
        scoreNode.getComponent(cc.Label).string = Math.floor(s).toString();
        this.schedule(() => {
            s += (score / 20);
            s = Math.min(s, score);
            scoreNode.getComponent(cc.Label).string = Math.floor(s).toString();
        }, 0.02, 20)

    }

    updateTime() {

        const gameType = DataMgr.Ins().getGameData('gameType') || GameType.normal;

        const win = DataMgr.Ins().getGameData('win');
        this.timeLeft.getComponent(cc.Sprite).spriteFrame = this.leftTimeSp[this.leftTime];
        SoundMgr.Ins().playEffect('game_leftTime', g_bundleName.gameBlock);
        console.log('updateTime', this.leftTime);
        if (this.leftTime == 0) {
            this.unscheduleAllCallbacks();
            switch (gameType) {
                case GameType.normal:
                    this.initScore();
                    break;
                case GameType.score:
                case GameType.step:
                case GameType.star:
                    this.initLevel(win)
                    break;
            }

        }
        this.leftTime--;

    }

    updateCycle() {
        this.timeLeftCycle.getComponent(cc.Sprite).fillRange = Math.max(this.timeLeftCycle.getComponent(cc.Sprite).fillRange - 0.0105, 0);

    }

    closeTimerCallBack() {
        const gameType = DataMgr.Ins().getGameData('gameType') || GameType.normal;
        const win = DataMgr.Ins().getGameData('win') || false;
        this.unscheduleAllCallbacks();
        switch (gameType) {
            case GameType.normal:
                this.initScore();
                break;
            case GameType.score:
            case GameType.step:
            case GameType.star:
                this.initLevel(win)
                break;
        }

    }


    reGameCallBack() {
        // this.unscheduleAllCallbacks();
        // NativeMgr.Ins().showAds();


        // this.node.removeFromParent();
        // this.node.destroy();
        // GameMgr.Ins().emit('reBlockGame');
    }

    newGameCallBack() {
        this.node.removeFromParent();
        this.node.destroy();
        GameMgr.Ins().emit('newBlockGame', true);

    }


    backHomeCallBack() {

        this.node.removeFromParent();
        this.node.destroy();
        GameMgr.Ins().emit('overGame');

    }

    nextLevelCallBack() {
        this.node.removeFromParent();
        this.node.destroy();
        GameMgr.Ins().emit('nextLevel', true);
    }

    onDestroy(): void {
        super.onDestroy();
        cc.systemEvent.targetOff(this);
    }
    // update (dt) {}
}
